No one really like serial numbers, but keeping track of them is one of the "brushing your teeth" activities that everyone needs to take care of. It's like eating your brussel sprouts. Or listening to your mom. You're just better of if you do it quickly as it just gets more painful over time.
Not only is it just good hygene, but you may be subject to regulations, like eRate in the United States where you have to be able to report on the location of any device by serial number at any point in time.
Trust me, having to play hide-and-go seek with an SSH session is not something you want to do when government auditors are looking for answers.
I'm sure you've already guessed what I'm about to say, but I"ll say it anyway...
There's an API for that!!!
HPE IMC base platform has a great network assets function that automatically gathers all the details of your various devices, assuming of course they support RFC 4133, otherwise known as the Entity MIB. On the bright side, most vendors have chosen to support this standards based MIB, so chances are you're in good shape.
And if they don't support it, they really should. You should ask them. Ok?
So without further ado, let's get started.
In [1]:
from pyhpeimc.auth import *
from pyhpeimc.plat.netassets import *
import csv
In [2]:
auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin")
In [3]:
ciscorouter = get_dev_asset_details('10.101.0.1', auth.creds, auth.url)
As some of you may have heard, HPE IMC is a multi-vendor tool and offers support for many of the common devices you'll see in your daily travels.
In this example, we're going to use a Cisco 2811 router to showcase the basic function.
Routers, like chassis switches have multiple components. As any one who's ever been the victem owner of a Smartnet contract, you'll know that you have individual components which have serial numbers as well and all of them have to be reported for them to be covered. So let's see if we managed to grab all of those by first checking out how many individual items we got back in the asset list for this cisco router.
In [4]:
len(ciscorouter)
Out[4]:
In [5]:
ciscorouter[0]
Out[5]:
With some basic python string manipulation we could easily print out some of the attributes that we want into what could easily turn into a nicely formated report.
Again realise that the example below is just a subset of what's available in the JSON above. If you want more, just add it to the list.
In [7]:
for i in ciscorouter:
print ("Device Name: " + i['deviceName'] + " Device Model: " + i['model'] +
"\nAsset Name is: " + i['name'] + " Asset Serial Number is: " +
i['serialNum']+ "\n")
Although we could go directly to the formated report without a lot of extra work, we would be losing a lot of data which we may have use for later. Instead why don't we export all the available data from the JSON above into a CSV file which can be later opened in your favourite spreadsheet viewer and manipulated to your hearst content.
Pretty cool, no?
In [9]:
keys = ciscorouter[0].keys()
with open('ciscorouter.csv', 'w') as file:
dict_writer = csv.DictWriter(file, keys)
dict_writer.writeheader()
dict_writer.writerows(ciscorouter)
Now we'll read it back from disk to make sure it worked properly. When working with data like this, I find it useful to think about who's going to be consuming the data. For example, when looking at this remember this is a CSV file which can be easily opened in python, or something like Microsoft Excel to manipuate further. It's not realy intended to be read by human beings in this particular format. You'll need another program to consume and munge the data first to turn it into something human consumable.
In [12]:
with open('ciscorouter.csv') as file:
print (file.read())
That's a great question! I'm glad you asked. One of the most beautiful things about learning to automate things like asset gathering through an API is that it's often not much more work to do something 1000 times than it is to do it a single time.
This time instead of using the get_dev_asset_details function that we used above which gets us all the assets associated with a single device, let's grab ALL the devices at once.
In [13]:
all_assets = get_dev_asset_details_all(auth.creds, auth.url)
In [14]:
len (all_assets)
Out[14]:
Exactly why we automate things. Now let's write the all_assets list to disk as well.
**note for reasons unknown to me at this time, although the majority of the assets have 27 differnet fields, a few of them actually have 28 different attributes. Something I'll have to dig into later.
In [15]:
keys = all_assets[0].keys()
with open('all_assets.csv', 'w') as file:
dict_writer = csv.DictWriter(file, keys)
dict_writer.writeheader()
dict_writer.writerows(all_assets)
In [16]:
print ("The length of the first items keys is " + str(len(keys)))
for i in all_assets:
if len(i) != len(all_assets[0].keys()):
print ("The length of index " + str(all_assets.index(i)) + " is " + str(len(i.keys())))
It looks like the items which don't have exactly 27 attribues have exactly 28 attributes. So we'll just pick one of the longer ones to use as the headers for our CSV file and then run the script again.
For this one, I'm going to ask you to trust me that the file is on disk and save us all the trouble of having to print out 1013 seperate assets into this blog post.
In [18]:
keys = all_assets[879].keys()
with open ('all_assets.csv', 'w') as file:
dict_writer = csv.DictWriter(file, keys)
dict_writer.writeheader()
dict_writer.writerows(all_assets)
So now that we've got all of our assets into a CSV file which is easily consumable by something like Excel, you can now chose what to do with the data.
For me it's interesting to see how vendors internalyl instrument their boxes. Some have serial numbers on power supplies and fans, some don't. Some use the standard way of doing things. Some don't.
From an operations perspective, not all gear is created equal and it's nice to understand what's supported when trying to make a purchasing choice for something you're going to have to live with for the next few years.
If you're looking at your annual SMARTnet upgrade, at least you've now got a way to easily audit all of your discovered environment and figure out what line cards need to be tied to a particualr contract.
Or you could just look at another vendor who makes your life easier. Entirely your choice.
@netmanchris